//
//  DataOption.cpp
//  Chart
//
//  Created by CaoJun on 13-5-31.
//
//

#include "DataOption.h"
#include "YTXChartTools.h"

#include <sstream>

#import <Foundation/Foundation.h>

using namespace ytx;
using namespace std;

const string DataOption::REQUEST_OPTION_TYPE = "get";
const string DataOption::REQUEST_OPTION_ROUTING = "";
const string DataOption::REQUEST_OPTION_ROUTINGSPACE = "";
const bool DataOption::REQUEST_OPTION_ISRandom = true;

DataOption::DataOption(map<string, string> option, string routing, string url, int timeout):option(option), routing(routing), timeout(timeout), url(url)
{

}


string DataOption::getUrl() const
{
    return this->routing;
}

//string DataOption::toString() const
//{
//    map<string, string> tempMap = this->option;
//    ostringstream result;
//    result << "?";
//    for (map<string, string>::iterator item = tempMap.begin(); item != tempMap.end(); item++) {
//        string key = (*item).first;
//        string value = (*item).second;
//        result << key << "=" << Tools::url_encode(value) << "&";
//    }
//    if (REQUEST_OPTION_ISRandom) {
//        result << "random=" << Tools::url_encode(Tools::random());
//    }
//    return result.str();
//}

NSDictionary* DataOption::toDictonary() const
{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    
    map<string, string> tempMap = this->option;

    for (map<string, string>::iterator item = tempMap.begin(); item != tempMap.end(); item++) {
        string key = (*item).first;
        string value = (*item).second;
        if (key == "Ei" || key == "QryTm" || key == "Lmt" || key == "QryDay"|| key == "QryTm"|| key == "QryEndTm" || key == "Period" || key == "DayNum") {
            dic[[NSString stringWithUTF8String:key.c_str()]] = @(Tools::parseLong(value));
        }
        else {
            dic[[NSString stringWithUTF8String:key.c_str()]] = [NSString stringWithUTF8String:value.c_str()];
        }
    }
    
    dic[@"random"] = [NSString stringWithUTF8String: Tools::url_encode(Tools::random()).c_str()];
    
    return dic;
}



